package com.ruoyi.web.controller.piliangruku;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.goods.domain.Shangpinxinxi;
import com.ruoyi.goods.service.IShangpinxinxiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.piliangruku.domain.PiliangRuku;
import com.ruoyi.piliangruku.service.IPiliangRukuService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 批量入库Controller
 *
 * @author ruoyi
 * @date 2023-05-19
 */
@Api(tags="批量入库管理")
@RestController
@RequestMapping("/system/ruku")
public class PiliangRukuController extends BaseController
{
    @Autowired
    private IPiliangRukuService piliangRukuService;

    @Autowired
    private IShangpinxinxiService shangpinxinxiService;

    /**
     * 查询批量入库列表
     */
    @ApiOperation("查询批量入库列表")
    @PreAuthorize("@ss.hasPermi('ghgl:plrk:list')")
    @GetMapping("/list")
    public TableDataInfo list(PiliangRuku piliangRuku)
    {
        startPage();
        List<PiliangRuku> list = piliangRukuService.selectPiliangRukuList(piliangRuku);
        return getDataTable(list);
    }

    /**
     * 导出批量入库列表
     */
//    @PreAuthorize("@ss.hasPermi('system:ruku:export')")
    @Log(title = "批量入库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PiliangRuku piliangRuku)
    {
        List<PiliangRuku> list = piliangRukuService.selectPiliangRukuList(piliangRuku);
        ExcelUtil<PiliangRuku> util = new ExcelUtil<PiliangRuku>(PiliangRuku.class);
        util.exportExcel(response, list, "批量入库数据");
    }

    /**
     * 获取批量入库详细信息
     */
    @ApiOperation("查询批量入库详细信息")
//    @PreAuthorize("@ss.hasPermi('system:ruku:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(piliangRukuService.selectPiliangRukuById(id));
    }

    /**
     * 新增批量入库
     */
    @ApiOperation("新增批量入库")
//    @PreAuthorize("@ss.hasPermi('system:ruku:add')")
    @Log(title = "批量入库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PiliangRuku piliangRuku)
    {
        return toAjax(piliangRukuService.insertPiliangRuku(piliangRuku));
    }

    /**
     * 扫码枪批量入库
     */
//    @PreAuthorize("@ss.hasPermi('system:ruku:add')")
    @Log(title = "扫码枪批量入库", businessType = BusinessType.INSERT)
    @PostMapping("/readinsert")
    public AjaxResult autoadd(@RequestBody PiliangRuku piliangRuku)
    {
        PiliangRuku plrk = new PiliangRuku();
        String lastghdh = piliangRukuService.selectGouhuoDanhaoByLastOne();
        System.out.println(lastghdh);
        System.out.println("--------------"+piliangRuku.getGoodId());

        int num;
        if (lastghdh == null || lastghdh.equals("")) {
            num = 1;
        }else {
            num = Integer.parseInt(lastghdh)+1;
        }

        String newghdh = String.format("%08d", num);
        String spid = piliangRuku.getGoodId().replace("\n","");

//        String newghdh = String.valueOf(num);
//        if (newghdh.length() < 8) {
//            int a = 8-newghdh.length();
//            for (int j = 0; j < a; j++) {
//                newghdh="0" + newghdh;
//            }
//        }


        plrk.setGouhuoDanhao(newghdh);

        plrk.setGouhuoDate(DateUtils.getNowDate());
        plrk.setPaymentMethod("0");
        plrk.setGouhuoJingshouren("");
        plrk.setGouhuoCangku("");
        plrk.setGoodId(piliangRuku.getGoodId().replace("\n",""));

        Shangpinxinxi sp  = shangpinxinxiService.selectShangpinxinxiByShangpinId(spid);
        if(sp==null){
          return  AjaxResult.error("商品不存在！");
        }
        plrk.setGoodName(sp.getShangpinName());

        if (piliangRuku.getGoodId().indexOf("，") >= 5||piliangRuku.getGoodId().indexOf(",")>=5) {
            String[] msgstr;
            if (piliangRuku.getGoodId().contains("，")) {
                msgstr = piliangRuku.getGoodId().split("，");
            }else{
                msgstr = piliangRuku.getGoodId().split(",");
            }

            String s = "";
            for (int i = 2; i < msgstr.length; i++) {
                s = s+msgstr[i];
            }
            plrk.setBeizhu(s);
        }



        plrk.setShuliang(BigDecimal.valueOf(1.0));


        return toAjax(piliangRukuService.insertPiliangRuku(plrk));
//        return null;
    }

    /**
     * 修改批量入库
     */
    @ApiOperation("修改批量入库")
    @PutMapping
//    @PreAuthorize("@ss.hasPermi('system:ruku:edit')")
    @Log(title = "批量入库", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@RequestBody PiliangRuku piliangRuku)
    {
        return toAjax(piliangRukuService.updatePiliangRuku(piliangRuku));
    }

    /**
     * 删除批量入库
     */
    @ApiOperation("删除批量入库")
//    @PreAuthorize("@ss.hasPermi('system:ruku:remove')")
    @Log(title = "批量入库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(piliangRukuService.deletePiliangRukuByIds(ids));
    }
}
